package com.church.action;

import java.util.Map;

import com.church.db.User;
import com.church.services.UserServices;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class Login extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 2L;
	private UserServices userServices;
	private String name;
	private String password;
	private String response;

	public UserServices getUserServices() {
		return userServices;
	}

	public void setUserServices(UserServices userServices) {
		this.userServices = userServices;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getResponse() {
		return response;
	}

	public void setResponse(String response) {
		this.response = response;
	}

	public void validate() {
		if (name == null) {
			addFieldError("name", "name is null!");
		}
		if (password == null) {
			addFieldError("password", "password is null!");
		}
	}

	public String execute() {
		User user = userServices.findUserByName(name);
		if(user!=null)
		{
			if(user.getPassword().equals(password))
			{
				response="success";
			}
			else
			{
				response="error";
			}
			if (response.equals("success")) {
				ActionContext actionContext = ActionContext.getContext();
				Map session = actionContext.getSession();
				session.put("USER_NAME", name);
				session.put("USER_TYPE", user.getType());
				session.put("USER_GROUP", user.getGroup());
			}
		}
		else
		{
			response="error";
		}

		return response;
	}

}
